﻿#pragma once

// 引入必要的头文件和 CTP 行情接口头文件
#include <define.h>
#include <market_api.h>
#include <event_center.hpp>
#include <mutex>
#include <condition_variable>
#include <params.hpp>
#include <../../api/CTP_V6.6.9_20220920/ThostFtdcMdApi.h>
#include <dll_helper.hpp>

// ctp_api_market 类
// 继承自 asyn_actual_market（异步市场接口类）和 CThostFtdcMdSpi（CTP 行情 SPI 接口实现类）
class ctp_api_market : public asyn_actual_market, public CThostFtdcMdSpi
{
public:
    // 构造函数：初始化行情 API 对象并加载相关配置
    // 参数：
    // - id_excg_map: 交易所 ID 映射
    // - config: 行情参数配置
    ctp_api_market(const std::shared_ptr<std::unordered_map<std::string, std::string>>& id_excg_map, const params& config) noexcept;

    // 析构函数：释放行情 API 资源
    virtual ~ctp_api_market() noexcept;

    // IMarketAPI 接口的实现
public:
    virtual bool login() noexcept override;                         // 登录
    virtual void logout() noexcept override;                       // 登出
    virtual void subscribe(const std::set<code_t>& codes) noexcept override;  // 订阅行情
    virtual void unsubscribe(const std::set<code_t>& codes) noexcept override; // 取消订阅行情

    // CThostFtdcMdSpi 接口的实现（用于处理来自 CTP 的回调通知）
public:
    virtual void OnRspError(CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) noexcept override; // 错误响应回调
    virtual void OnFrontConnected() noexcept override; // 连接成功回调
    virtual void OnRspUserLogin(CThostFtdcRspUserLoginField* pRspUserLogin, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) noexcept override; // 登录响应回调
    virtual void OnRspUserLogout(CThostFtdcUserLogoutField* pUserLogout, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) noexcept override; // 登出响应回调
    virtual void OnFrontDisconnected(int nReason) noexcept override; // 连接断开回调
    virtual void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField* pDepthMarketData) noexcept override; // 深度行情数据回调
    virtual void OnRspSubMarketData(CThostFtdcSpecificInstrumentField* pSpecificInstrument, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) noexcept override; // 订阅行情响应回调
    virtual void OnRspUnSubMarketData(CThostFtdcSpecificInstrumentField* pSpecificInstrument, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) noexcept override; // 取消订阅行情响应回调

private:
    // 私有方法：执行登录请求
    void do_userlogin() noexcept;

    // 私有方法：执行订阅行情
    void do_subscribe() noexcept;

    // 私有方法：执行取消订阅行情
    void do_unsubscribe(const std::vector<code_t>& code_list) noexcept;

private:
    // 成员变量
    CThostFtdcMdApi* _md_api;                  // CTP 行情 API 指针
    std::string _front_addr;                   // 前置地址
    std::string _broker_id;                    // 经纪商 ID
    std::string _userid;                       // 用户 ID
    std::string _password;                     // 密码
    int _reqid;                                // 请求 ID

    std::mutex _mutex;                         // 互斥锁
    std::unique_lock<std::mutex> _process_mutex; // 进程互斥锁
    std::condition_variable _process_signal;   // 进程信号量

    bool _is_inited;                           // 是否已初始化

    typedef CThostFtdcMdApi* (*market_creator)(const char*, const bool, const bool); // 定义行情 API 创建函数指针
    market_creator _ctp_creator;               // CTP 创建函数指针
    dll_handle _market_handle;                 // 动态库句柄
};
